<?php

/* 
 * 取首页广告数据
 */
$rec=isset($_GET['rec'])?$_GET['rec']:"";
if($rec=="file"){   
        
        $path = "uploads/";
        $extArr = array("jpg", "png", "gif");
	
        $name = $_FILES['photoimg']['name'];
	$size = $_FILES['photoimg']['size'];
	
	if(empty($name)){
		echo '请选择要上传的图片';
		exit;
	}

	$ext=extend($name);
        
	if(!in_array($ext,$extArr)){
		echo '图片格式错误！';
		exit;
	}
        
	if($size>(2000*1024)){
		echo '图片大小不能超过2000KB';
		exit;
	}
	$image_name = time().rand(100,999).".".$ext;
	$tmp = $_FILES['photoimg']['tmp_name'];
        
	if(move_uploaded_file($tmp, $path.$image_name)){
                echo '<input value="'.$path.$image_name.'"  name="imgurl" class="inpMain" />';
		echo '<img src="'.$path.$image_name.'"  class="preview">';
	}else{
		echo '上传出错了！';
	}
	exit;
}


require("../db_conn.php");

if ($rec=='insert')
{
    $adname=$_POST['adname'];
    $linkurl=$_POST['linkurl'];
    $isshow=$_POST['isshow'];
    $sql="insert into tb_ad(adname,linkurl,isshow) values('$adname','$linkurl',$isshow)";
    $rs=$db->query($sql);
    if ($rs==FALSE)
    {
        echo "<script language='javascript' type='text/javascript'>";
        echo "alert('添加失败！');";
        echo "window.location.href='show.php'";
        echo "</script>";
    }
}


$sql="SELECT * FROM `tb_ad`  where isshow=1   order by id desc limit 0,5 ";   //写查询语句  isshow=1   取5个    
$adrs=$db->query($sql); //执行语句  $rs是一个结果集
$adrows=$adrs->fetch_all(MYSQLI_BOTH);   //包括所有行 $adrows有数据库的焦点广告
require 'show_html.php';
//释放结果集
$adrs->free();
//关闭数据库
$db->close();



function extend($file_name){
	$extend = pathinfo($file_name);
	$extend = strtolower($extend["extension"]);
	return $extend;
}


